﻿using System;
using System.Data.SqlClient;

/// <summary>
/// Summary description for UserManager
/// </summary>
public class UserManager
{
    SqlConnection conn = null;
	public UserManager()
	{
		//
		// TODO: Add constructor logic here
		//
	}

    //
    // InsertUser
    //
    public bool InsertUser(User objUser) {
        try{
            conn = MapConnection.GetConnection();
            //string sqlInsert = "INSERT INTO User VALUES (@username, @firstname, @lastname, @email, @phone, @password, @sex, @birthday)";
            string sqlInsert = "INSERT INTO Users(username,email, password, birthday) VALUES (@username,@email, @password, '08/04/1983')";
            SqlCommand command = new SqlCommand(sqlInsert, conn);
            command.Parameters.AddWithValue("@username", objUser.UserName);
            command.Parameters.AddWithValue("@email", objUser.Email);
            command.Parameters.AddWithValue("@password", objUser.Password);

            command.ExecuteNonQuery();
            return true;
        }
        catch (Exception ex) {
            Console.WriteLine(ex.StackTrace);
        }
        finally{
            MapConnection.CloseConnetion(conn);
        }
        return false;
    }

    //
    // GetUser
    //
    public bool GetUser(User objUser) {
        bool flag = false;
        try{
            conn = MapConnection.GetConnection();
            string sqlSelect = "SELECT username, password FROM User WHERE username = @username and password = @password";

            SqlCommand command = new SqlCommand(sqlSelect, conn);
            command.Parameters.AddWithValue("@username", objUser.UserName);
            command.Parameters.AddWithValue("@password", objUser.Password);
            SqlDataReader data = command.ExecuteReader();

            data.Read();            

            string username = data.GetString(1);
            string password = data.GetString(2);
            //user = new User(username, password);
            flag = true;
        }
        catch (Exception ex){
            Console.WriteLine(ex.StackTrace);
            flag = false;
        }
        finally {
            MapConnection.CloseConnetion(conn);
        }
        return flag;
    }


    public bool DeleteUser(string username) {
        bool flag = false;
        try
        {
            conn = MapConnection.GetConnection();
            string sqlDelete = "DELETE FROM User WHERE username = @username";

            SqlCommand command = new SqlCommand(sqlDelete, conn);
            command.Parameters.AddWithValue("@username", username);
            command.ExecuteNonQuery();            

            flag = true;
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.StackTrace);
            flag = false;
        }
        finally
        {
            MapConnection.CloseConnetion(conn);
        }                            
        return flag;
    }
}
